#include<iostream>
#include <cstring>
using namespace std;
 
template<typename T>
class Matrix
{
private:
    T **Max;
    int row,col;
public:
    Matrix():row(0),col(0){}
    Matrix(T** M,int m,int n)
    {
        int i,j;
        row=m,col=n;
        Max=new T*[row];
        for( i=0;i<row;i++)
            Max[i]=new T [col];
  
        for( i=0;i<row;i++)
          for( j=0;j<col;j++)
            Max[i][j]=M[i][j];
    }
    Matrix(Matrix &M)
    {
        for(int i=0;i<row;i++)
          for(int j=0;j<col;j++)
            Max[i][j]=M.Max[i][j];
    }
    void transport()
    {
        int i,j;
        T **M;
        M=new T*[row];
        for( i=0;i<row;i++)
            M[i]=new T [col];
 
        for( i=0;i<row;i++)
          for( j=0;j<col;j++)
            M[i][j]=Max[i][j];
         
         for(i=0;i<row;i++)
          delete [] Max[i];
        delete [] Max;
 
        int temp = row;
        row=col;
        col=temp;
 
        Max=new T*[row];
        for( i=0;i<row;i++)
            Max[i]=new T [col];
 
        for( i=0;i<row;i++)
          for( j=0;j<col;j++)
            Max[i][j]=M[j][i];
 
         for(i=0;i<col;i++)
          delete [] M[i];
        delete [] M;
 
    }
    void print()
    {
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col-1;j++)
             cout<<Max[i][j]<<" ";
            cout<<Max[i][col-1]<<endl;
        }
    }
    ~Matrix()
    {
        for(int i=0;i<row;i++)
          delete [] Max[i];
        delete [] Max;
    }
};
 
int main()
{
    int m,n,times,
        i,j;
    char ch;
    int** m1;
    double** m2;
    char** m3;
     
    cin>>times;
    while(times--)
    {
        cin>>ch;
        cin>>m>>n;
  
        if(ch=='I')
        {
            m1=new int*[m];
            for( i=0;i<m;i++)
                m1[i]=new int [n];
  
            for(i=0;i<m;i++)
                for( j=0;j<n;j++)
            cin>>m1[i][j];
  
            Matrix<int>Ma1(m1,m,n);
            Ma1.transport();
            Ma1.print();
 
            for( i=0;i<m;i++)
              delete [] m1[i];
            delete [] m1;
        }
        if(ch=='D')
        {
            m2=new double*[m];
            for( i=0;i<m;i++)
                m2[i]=new double [n];
  
            for( i=0;i<m;i++)
                for( j=0;j<n;j++)
                cin>>m2[i][j];
  
            Matrix<double>Ma1(m2,m,n);
            Ma1.transport();
            Ma1.print();
 
            for( i=0;i<m;i++)
              delete [] m2[i];
            delete [] m2;
        }
        if(ch=='C')
        {
            m3=new char*[m];
            for(int i=0;i<m;i++)
                m3[i]=new char [n];
  
            for( i=0;i<m;i++)
                for( j=0;j<n;j++)
            cin>>m3[i][j];
  
            Matrix<char>Ma1(m3,m,n);
            Ma1.transport();
            Ma1.print();
  
            for( i=0;i<m;i++)
              delete [] m3[i];
            delete [] m3;
        }
    }
    return 0; 
}
